package com.atguigu.eduservice.controller;

import com.alibaba.excel.EasyExcel;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.listener.SubjectListener;
import com.atguigu.eduservice.service.SubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/eduservice/subject")
@CrossOrigin
public class EduSubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 先上传文件，后端保存到数据库
     * @param file
     * @return
     */
    @PostMapping("/addSubject")
    public R saveFile(MultipartFile file)
    {
        try {
            InputStream is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class,new SubjectListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            System.out.println("获取文件错误"+e);
        }
        return R.ok();
    }

    /**
     * 获取所有的课程
     * @return
     * 树形结构
     */
    @GetMapping("/getAllSubject")
    public R getAllSubject()
    {
        List<OneSubject> list=subjectService.getAllSubject();
        return R.ok().data("list",list);
    }

}
